package com.cloud.servlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cloud.core.BaseServlet;
import com.cloud.dao.CaseBean;
import com.cloud.dao.DoctorBean;
import com.cloud.dao.PictureBean;
import com.cloud.dao.UserBean;
import com.cloud.service.CaseService;
import com.cloud.service.DoctorService;
import com.cloud.service.FileService;
import com.cloud.service.UserService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;
import com.cloud.util.Picture;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-11-14
 * @version 1.0 
 */
@Controller
@RequestMapping("/file")
public class FileServlet extends BaseServlet {
	private static final Logger logger = LoggerFactory.getLogger(FileServlet.class);
	@Autowired
	private FileService FileService;
	@Autowired
	private CaseService CaseService;
	@Autowired
	private UserService UserService;
	@Autowired
	private DoctorService DoctorService;
	
	/**
	 * 病例图片上传
	 * @param request
	 * @param casid
	 * @param files
	 * @return
	 */
	@RequestMapping(value = "/case/{casid}/upload", method = { RequestMethod.POST})
	public @ResponseBody Object upload(HttpServletRequest request, @PathVariable String casid, @RequestParam MultipartFile[] files) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(casid) || files == null){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		if(files.length > 0){
			String[] ids = FileService.uploadPicture(files, Picture.CASE);
			for(String id : ids){
				CaseBean bcase = new CaseBean();
				bcase.setCase_id(casid);
				bcase.setPic_id(id);
				CaseService.addCasePicture(bcase);
			}
			map.put("code", Code.C200.key);
			map.put("msg", Code.C200.desc);
			map.put("data", ids);
		} else {
			map.put("code", Code.C440.key);
			map.put("msg", Code.C440.desc);
		}
		return map;
	}
	
	/**
	 * 病例图片删除
	 * @param request
	 * @param casid
	 * @param picid
	 * @return
	 */
	@RequestMapping(value = "/{casid}/{picid}/delete", method = { RequestMethod.POST})
	public @ResponseBody Object delete(HttpServletRequest request, @PathVariable String casid, @PathVariable String picid) {
		for(String _picId : picid.split(",")){
			CaseBean bcase = new CaseBean();
			bcase.setCase_id(casid);
			bcase.setPic_id(_picId);
			CaseService.deletePicture(bcase);
			PictureBean bean = new PictureBean();
			bean.setPicId(_picId);
			List<PictureBean> pbeanList = FileService.getPictureList(bean);
			for(PictureBean _bean : pbeanList){
				FileService.deletePicture(_bean);
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	@RequestMapping(value = "/head/{userid}/update", method = { RequestMethod.POST})
	public @ResponseBody Object hupload(HttpServletRequest request, @PathVariable String userid, @RequestParam MultipartFile file) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(userid) || file == null || file.isEmpty()){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		UserBean user = new UserBean();
		user.setUserId(userid);
		String crcUrl = "";
		List<UserBean> retUserList = UserService.getUserList(user);
		if(retUserList != null && retUserList.size() > 0){
			crcUrl = retUserList.get(0).getCrcurl();
		}
		logger.debug("========修改头像===========" + crcUrl);
		if(CommUtil.isEmpty(crcUrl)){
			String id = FileService.uploadPicture(file, Picture.HEAD);
			user.setCrc(id);
			UserService.updateUser(user, true);
		} else {
			FileService.uploadPicture(file, crcUrl);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	@RequestMapping(value = "/license/{doctorid}/update", method = { RequestMethod.POST})
	public @ResponseBody Object license(HttpServletRequest request, @PathVariable String doctorid, @RequestParam MultipartFile file) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(doctorid) || file == null || file.isEmpty()){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		DoctorBean doctor = new DoctorBean();
		doctor.setId(doctorid);
		String license = "";
		List<DoctorBean> retDoctorList = DoctorService.getDoctorList(doctor);
		if(retDoctorList != null && retDoctorList.size() > 0){
			license = retDoctorList.get(0).getLicense_pics();
		}
		logger.debug("=========修改执照==========" + license);
		if(CommUtil.isEmpty(license)){
			String id = FileService.uploadPicture(file, Picture.LICENSE);
			doctor.setLicense_pics(id);
			DoctorService.updateDoctor(doctor);
		} else {
			FileService.uploadPicture(file, license);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
}
